package com.bianguo.android.baselibrary.adapter;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.bianguo.android.ioclibrary.ButterKnife;

import java.util.ArrayList;
import java.util.Collection;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> {

    protected Context mContext;
    protected ArrayList<T> list;
    protected LayoutInflater inflater;
    protected OnClickItemListener onClickItemListener;
    protected boolean openLoadAnim;

    public BaseRecyclerAdapter(Context mContext, ArrayList<T> list) {
        this.mContext = mContext;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return createHolder(viewGroup, i);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
        bindHolder(baseViewHolder, i);
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public void onViewAttachedToWindow(@NonNull BaseViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (openLoadAnim) {
            loadAnim(holder);
        }
    }

    protected void loadAnim(@NonNull BaseViewHolder holder) {
        AnimatorSet scaleAnim = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX", 0.9f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 0.9f, 1f);
        ObjectAnimator tranY = ObjectAnimator.ofFloat(holder.itemView, "translationY", 20, 0);
        scaleX.setInterpolator(new LinearInterpolator());
        scaleY.setInterpolator(new LinearInterpolator());
        scaleAnim.setDuration(300);
        scaleAnim.playTogether(scaleX, scaleY, tranY);
        scaleAnim.start();
        scaleAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                holder.itemView.clearAnimation();
                animation = null;
                System.gc();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    public void addAll(Collection<? extends T> collections, boolean isRefresh) {
        if (isRefresh) {
            list.clear();
        }
        list.addAll(collections);
        notifyDataSetChanged();
    }

    public abstract BaseViewHolder createHolder(@NonNull ViewGroup viewGroup, int viewType);

    public abstract void bindHolder(@NonNull BaseViewHolder baseViewHolder, int position);

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public ArrayList<T> getList() {
        return list;
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }

    public interface OnClickItemListener {

        void onClickItem(BaseRecyclerAdapter adapter, View view, int position, int tag);
    }
}
